﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Newtonsoft.Json;

namespace AUV5.DAL.Handler
{
    public class ServiceHandler
    {
        public IList<Model.ServiceInfo> servicelist = new List<Model.ServiceInfo>();
        public StringBuilder json = new StringBuilder();

        public void GetFullList(object sender, object parameterObj, IList list)
        {
            Model.ServiceInfo service = GetFullService(sender);
            if (service != null)
            {
                servicelist.Add(service);
            }
        }

        public void ParseFullJson(object sender, object parameterObj, IList list)
        {
            json.Append(JsonConvert.SerializeObject(GetFullService(sender)));
        }

        public Model.ServiceInfo GetFullService(object sender)
        {
            try
            {
                if (sender != null)
                {
                    //获取用信息
                    Model.ServiceInfo service = (Model.ServiceInfo)sender;
                    Model.UserInfo user = new Model.UserInfo();
                    user.UserID = service.UserID;
                    Model.UserInfo seller = Dao.UserInfoSqlMapDao.Instance.GetDetail(user);
                    if (seller != null)
                    {
                        service.User = seller;
                        service.UserName = service.User.UserName;
                    }
                    else
                    {
                        //删除错误数据
                        //有风险啊有风险
                        Dao.ServiceInfoSqlMapDao.Instance.Delete(service);
                    }
                    //获取类别名
                    Model.ServiceCategory category = new Model.ServiceCategory();
                    category.CategoryID = service.CategoryID;
                    Model.ServiceCategory servicecategory = Dao.ServiceCategorySqlMapDao.Instance.GetDetail(category);
                    if (servicecategory != null)
                    {
                        service.CategoryName = servicecategory.CategoryName;
                    }
                    else
                    {
                        //删除错误数据
                        Dao.ServiceInfoSqlMapDao.Instance.Delete(service);
                    }

                    //获取附件列表
                    Hashtable ht = new Hashtable();
                    ht.Add("TopCount", 4);
                    ht.Add("ServiceID", service.ServiceID);
                    ht.Add("State", (int)Common.Enum.auvState.Enabled);
                    service.AttachList = Dao.ServiceAttachmentSqlMapDao.Instance.GetListByParameters(ht);
                    //获取关键字
                    ht.Clear();
                    ht.Add("TopCounts", 10);
                    ht.Add("serviceID", service.ServiceID);
                    ht.Add("state", (int)Common.Enum.auvState.Enabled);
                    //获取的是State=1的值
                    IList<Model.ServiceKeyRela> list = Dao.ServiceKeyRelaSqlMapDao.Instance.GetListByParameters(ht);
                    Model.Keyword key = new Model.Keyword();
                    IList<string> keywords = new List<string>();
                    foreach (var item in list)
                    {
                        key.KeyID = item.KeyID;
                        keywords.Add(Dao.KeywordSqlMapDao.Instance.GetDetail(key).Content);
                    }
                    service.KeyWords = keywords;

                    //增值服务

                    ht.Clear();
                    ht.Add("serviceID", service.ServiceID);
                    ht.Add("state", (int)Common.Enum.auvState.Enabled);
                    ht.Add("isDel", (int)Common.Enum.auvDel.NotDel);

                    service.ExtraList = Dao.ExtraServiceSqlMapDao.Instance.GetListByParameters(ht);

                    return service;
                }
                return null;
            }
            catch (Exception e)
            {
                throw new Exception("Error executing ServiceHandler,  Cause: " + e.Message, e);
            }


        }
    }
}
